//缩放
var x, y; // 获取鼠标所处位置参数
var zoomRate = 1.5;// 缩放参数
var currentzR; // 确定放大or缩小
var x1, x2, y1, y2;// svg的屏幕坐标
var vb0, vb1, vb2, vb3;// viewbox的4个参数
var c = 0;
var point;
var svgElement;

function coordinateTransform(screenPoint, someSvgObject) {
	var CTM = someSvgObject.getScreenCTM(); // 将屏幕坐标转换为视口坐标
	return screenPoint.matrixTransform(CTM.inverse());
}

function trans(screenPoint, someSvgObject)// 将视口坐标转换为屏幕坐标
{
	var CTM = someSvgObject.getScreenCTM();
	return screenPoint.matrixTransform(CTM);
}

function Scale() { // 这是一个滚轮响应事件
	var svgElement = document.getElementById('SvgElement');
	var screenPoint = svgElement.createSVGPoint();
	screenPoint.x = event.pageX;// clientX相对于快的坐标，pageX相对于文档的坐标
	screenPoint.y = event.pageY;
	
	var svgPoint = coordinateTransform(screenPoint, svgElement);

	x = svgPoint.x;
	y = svgPoint.y;

	var viewBox = svgElement.getAttribute('viewBox');
	var viewBoxValues = viewBox.split(' ');
	vb0 = parseFloat(viewBoxValues[0]); // 获取viewbox的4个参数，并将4个参数转换为float型
	vb1 = parseFloat(viewBoxValues[1]);
	vb2 = parseFloat(viewBoxValues[2]);
	vb3 = parseFloat(viewBoxValues[3]);

	// 放大------------------------------------------------------------------------------
	if (event.wheelDelta / 12 > 0) {// 滚轮向上滚
		currentzR = zoomRate;
	}
	// 缩小------------------------------------------------------------------------------
	else {
		currentzR = 1 / zoomRate;
	}
	viewBoxValues[0] = x - (x - vb0) / currentzR;
	viewBoxValues[1] = y - (y - vb1) / currentzR;
	viewBoxValues[2] = vb2 / currentzR;
	viewBoxValues[3] = vb3 / currentzR;

	svgElement.setAttribute('viewBox', viewBoxValues.join(' '));// 将修改的参数保存
	ChartPositionList();

	X = point.x;
	Y = point.y;

}

//平移
var mousePos;
var spx;
var spy;
var dvx,dvy,tx,ty,tx2,ty2;
var w=false;
function mouseMove(ev) {
	var svgElement = document.getElementById('SvgElement');
	var viewBox = svgElement.getAttribute('viewBox');
	var viewBoxValues = viewBox.split(' ');
	var tempP1 = svgElement.createSVGPoint();
	var tempP2 = svgElement.createSVGPoint();
	
	Ev = ev || window.event;
	mousePos = mouseCoords(ev);
	
	tempP1.x = spx;
	tempP1.y = spy;
	tempP1 = coordinateTransform(tempP1,svgElement);
	spx = mousePos.x;
	spy = mousePos.y;
	tempP2.x = mousePos.x;
	tempP2.y = mousePos.y;
	tempP2 = coordinateTransform(tempP2,svgElement);
	dvx = tempP2.x-tempP1.x;
	dvy = tempP2.y-tempP1.y;	
	vb00 = parseFloat(viewBoxValues[0]); // 获取viewbox的4个参数，并将4个参数转换为float型
	vb11 = parseFloat(viewBoxValues[1]);
	vb22 = parseFloat(viewBoxValues[2]);
	vb33 = parseFloat(viewBoxValues[3]);
	if(w==true){
		viewBoxValues[0] = vb00-dvx;
		viewBoxValues[1] = vb11-dvy;
		svgElement.setAttribute('viewBox', viewBoxValues.join(' '));// 将修改的参数保存
		ChartPositionList();
	}
}
function mouseCoords(ev) {
	if (ev.pageX || ev.pageY) {
		return {
			x : ev.pageX,
			y : ev.pageY
		};
	}
	return {
		x : ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y : ev.clientY + document.body.scrollTop - document.body.clientTop
	};
}

function Pan()
{
	spx = event.pageX;
	spy = event.pageY;
	tx =spx;
	ty =spy;
	w=true;
}

function changeW()
{
	tx2 = event.pageX;
	ty2 = event.pageY;
	w=false;
	X=X+tx2-tx;
	Y=Y+ty2-ty;
	var str="怀柔县";

	AddChart(str);
}

document.onmousemove = mouseMove;
document.onmouseup = changeW;